package com.kelaite.procurement.service.impl;

import io.jsonwebtoken.lang.Collections;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.constants.Constants;
import com.kelaite.procurement.dto.CompanyDTO;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TIndustry;
import com.kelaite.procurement.exception.BusinessException;
import com.kelaite.procurement.repository.CompanyRepository;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.utils.ConvertUtil;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyRepository companyRepository;

	@PersistenceContext
	private EntityManager em;

	@Override
	public TCompany save(TCompany persistence) {

		return this.companyRepository.save(persistence);
	}

	@Override
	public TCompany getObject(Long id) {

		return this.companyRepository.findOne(id);
	}

	@Override
	public List<TCompany> getAll() {

		return this.companyRepository.findAll();
	}

	@Override
	public void deleteByObject(TCompany persistence) {

		this.companyRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {

		this.companyRepository.delete(id);
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<TCompany> getByProperty(String property, Object value) {

		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TCompany.class).add(
				org.hibernate.criterion.Expression.eq(property, value));
		return criteria.list();
	}

	@Override
	public List<TCompany> findByBusinessType(short id) {

		return companyRepository.findByBusinessType(id);
	}

	@Override
	public TCompany findByCompanyName(String companyName) {

		return companyRepository.findByCompanyName(companyName);
	}

	@Override
	public List<TCompany> findCompanyByIndustry(List<TIndustry> industryList) {

		List<TCompany> targetCompanyList = new ArrayList<TCompany>();
		List<TCompany> companyList = this
				.findByBusinessType(Short.parseShort(String
						.valueOf(Constants.COMPANY_BUSINESS_TYPE_PARTY_SECOND)));
		for (TIndustry industry : industryList) {
			for (TCompany company : companyList) {
				if (company.getMainBusiness() != null
						&& company.getMainBusiness().contains(
								industry.getIndustryName())) {
					targetCompanyList.add(company);
				}
			}
		}
		return targetCompanyList;
	}

	@Override
	public List<CompanyDTO> findSubCompanyList(CompanyDTO companyDto) {

		if (null == companyDto || companyDto.getId() == null) {
			throw new BusinessException("company is invalid. ");
		}
		
		Long groupId = companyDto.getGroupId();
		if(groupId == null || groupId <= 0L){
			//集團公司
			groupId = companyDto.getId();
		}
		List<TCompany> dbList = companyRepository.findByGroupIdAndIsDeleted(groupId, Constants.UNDELETED);
		if(Collections.isEmpty(dbList)){
			List<CompanyDTO> rt = new ArrayList<CompanyDTO>();
			rt.add(companyDto);
			return rt;
		}
		return ConvertUtil.convertList(dbList, CompanyDTO.class);
	}

}
